package com.morn.weibo.types;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.j256.ormlite.field.DatabaseField;
import com.morn.weibo.exception.WeiboException;
import com.morn.weibo.utils.DateUtils;

public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3153237328220143423L;

	@DatabaseField(id = true)
	private String id;

	@DatabaseField
	private String name;

	@DatabaseField
	private String screen_name;

	@DatabaseField
	private String location;

	@DatabaseField
	private String gender;

	@DatabaseField
	private Date birthday;

	@DatabaseField
	private String description;

	@DatabaseField
	private String profile_image_url;

	@DatabaseField
	private String profile_image_url_large;

	@DatabaseField
	private String url;

	@DatabaseField
	private boolean protected_;

	@DatabaseField
	private int friends_count;

	@DatabaseField
	private int followers_count;

	@DatabaseField
	private int favourites_count;

	@DatabaseField
	private int statuses_count;

	@DatabaseField
	private boolean following;

	@DatabaseField
	private boolean notifications;

	@DatabaseField
	private Date created_at;

	@DatabaseField
	private String utc_offset;

	@DatabaseField
	private String firstSpellName;

	@DatabaseField
	private String spellName;

	@DatabaseField(foreign = true)
	private Status status;

	public User() {

	}

	public User(JSONObject json) throws WeiboException {
		init(json);

	}

	private void init(JSONObject json) throws WeiboException {
		if (json != null) {
			try {
				id = json.getString("id");
				name = json.getString("name");
				// if (name != null && !"".equals(name)) {
				// firstSpellName = PinyinToolkit.cn2FirstSpell(name);
				// spellName = PinyinToolkit.cn2Spell(name);
				// }
				screen_name = json.getString("screen_name");
				location = json.getString("location");
				gender = json.getString("gender");
				// 2006-03-02
				birthday = DateUtils.parseDate(json.getString("birthday"),
						"yyyy-MM-dd");
				description = json.getString("description");
				profile_image_url = json.getString("profile_image_url");
				profile_image_url_large = json
						.getString("profile_image_url_large");
				url = json.getString("url");
				protected_ = json.getBoolean("protected");
				friends_count = json.getInt("friends_count");
				followers_count = json.getInt("followers_count");
				favourites_count = json.getInt("favourites_count");
				statuses_count = json.getInt("statuses_count");
				following = json.getBoolean("following");
				notifications = json.getBoolean("notifications");
				// Fri Apr 17 01:34:06 +0000 2009
				created_at = DateUtils.parseDate(json.getString("created_at"),
						"EEE MMM dd HH:mm:ss ZZZZZ yyyy");
				utc_offset = json.getString("utc_offset");
				if (!json.isNull("status")) {
					status = new Status(json.getJSONObject("status"));
				}
			} catch (JSONException e) {
				throw new WeiboException(
						e.getMessage() + ":" + json.toString(), e);
			}
		}

	}

	public static List<User> getUserList(JSONArray array)
			throws WeiboException, JSONException {

		List<User> list = new ArrayList<User>();

		int size = array.length();
		for (int i = 0; i < size; i++) {
			User temp = new User(array.getJSONObject(i));
			list.add(temp);

		}

		return list;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getScreen_name() {
		return screen_name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setScreen_name(String screen_name) {
		this.screen_name = screen_name;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setProfile_image_url(String profile_image_url) {
		this.profile_image_url = profile_image_url;
	}

	public String getProfile_image_url_large() {
		return profile_image_url_large;
	}

	public void setProfile_image_url_large(String profile_image_url_large) {
		this.profile_image_url_large = profile_image_url_large;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setProtected_(boolean protected_) {
		this.protected_ = protected_;
	}

	public void setFriends_count(int friends_count) {
		this.friends_count = friends_count;
	}

	public void setFollowers_count(int followers_count) {
		this.followers_count = followers_count;
	}

	public void setFavourites_count(int favourites_count) {
		this.favourites_count = favourites_count;
	}

	public void setStatuses_count(int statuses_count) {
		this.statuses_count = statuses_count;
	}

	public void setFollowing(boolean following) {
		this.following = following;
	}

	public void setNotifications(boolean notifications) {
		this.notifications = notifications;
	}

	public void setCreated_at(Date created_at) {
		this.created_at = created_at;
	}

	public void setUtc_offset(String utc_offset) {
		this.utc_offset = utc_offset;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public String getLocation() {
		return location;
	}

	public String getGender() {
		return gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public String getDescription() {
		return description;
	}

	public String getProfile_image_url() {
		return profile_image_url;
	}

	public String getUrl() {
		return url;
	}

	public boolean isProtected_() {
		return protected_;
	}

	public int getFriends_count() {
		return friends_count;
	}

	public int getFollowers_count() {
		return followers_count;
	}

	public int getFavourites_count() {
		return favourites_count;
	}

	public int getStatuses_count() {
		return statuses_count;
	}

	public boolean isFollowing() {
		return following;
	}

	public boolean isNotifications() {
		return notifications;
	}

	public Date getCreated_at() {
		return created_at;
	}

	public String getUtc_offset() {
		return utc_offset;
	}

	public Status getStatus() {
		return status;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFirstSpellName() {
		return firstSpellName;
	}

	public void setFirstSpellName(String firstSpellName) {
		this.firstSpellName = firstSpellName;
	}

	public String getSpellName() {
		return spellName;
	}

	public void setSpellName(String spellName) {
		this.spellName = spellName;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", screen_name="
				+ screen_name + ", location=" + location + ", gender=" + gender
				+ ", birthday=" + birthday + ", description=" + description
				+ ", profile_image_url=" + profile_image_url + ", url=" + url
				+ ", protected_=" + protected_ + ", friends_count="
				+ friends_count + ", followers_count=" + followers_count
				+ ", favourites_count=" + favourites_count
				+ ", statuses_count=" + statuses_count + ", following="
				+ following + ", notifications=" + notifications
				+ ", created_at=" + created_at + ", utc_offset=" + utc_offset
				+ ", firstSpellName=" + firstSpellName + ", spellName="
				+ spellName + ", status=" + status + "]";
	}

}
